<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  function arr(nums) {
    let res = 0, start = 0, end = 1;
    while (end < nums.length) {
      let max = 0;
      for (let i = start; i < end; ++i) {
        max = Math.max(i + nums[i], max);
      }
      start = end;
      end = max + 1;
      ++res;
    }
    return res;
  };
  console.log(arr(nums = [2, 3, 1, 1, 4]));
</script>

</html>